package cn.com.yusys.yusp.admin.web.rest;

import cn.com.yusys.yusp.admin.domain.AdminSmMessage;
import cn.com.yusys.yusp.admin.service.AdminSmMessageService;
import cn.com.yusys.yusp.commons.exception.MessageConstants;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.web.rest.CommonResource;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URISyntaxException;


/**
 * @version 1.0.0
 * @项目名称: yusp-admin
 * @类名称: AdminSmMessageResource
 * @类描述: 系统提示消息
 * @功能描述:
 * @创建人: liaoxd@yusys.com.cn
 * @创建时间: 2017-12-27 17:51
 * @修改备注:
 * @修改记录: 修改时间    修改人员    修改原因
 * -------------------------------------------------------------
 * @Copyright (c) 2017宇信科技-版权所有
 */
@RestController
@RequestMapping("/api/adminsmmessage")
public class AdminSmMessageResource extends CommonResource<AdminSmMessage, String> {

    @Autowired
    private AdminSmMessageService adminSmMessageService;

    private final Logger log = LoggerFactory.getLogger(AdminSmMessageResource.class);

    @Override
    protected CommonService getCommonService() {
        // TODO Auto-generated method stub
        return this.adminSmMessageService;
    }


    /**
     * @方法名称:deleteMult
     * @方法描述:多个删除消息
     * @参数与返回说明:
     * @算法描述:
     */
    @PostMapping("/deletes/{ids}")
    public ResultDto<Integer> deleteMult(
            @ApiParam(value = "Object Of PK id", required = true) @PathVariable String ids) {
        log.debug("REST request to delete Object : {}", ids);
        String[] id = ids.toString().split(",");
        int i;
        ResultDto<Integer> resultDto = new ResultDto<>();
        try {
            for (i = 0; i < id.length; i++) {
                this.adminSmMessageService.deleteWithCache(id[i]);
            }
            resultDto.setData(i);
            resultDto.setMessage("批量删除系统提示信息成功!");
        } catch (Exception e) {
            resultDto.setCode(500);
            resultDto.setMessage(e.getMessage());
        }
        return resultDto;

    }

    /**
     * @方法名称: updateSelective
     * @方法描述: 修改接口
     * @参数与返回说明:
     * @算法描述:
     */
    @PostMapping("/updates")
    public ResultDto<AdminSmMessage> updateSelective(@ApiParam(value = "Object", required = true) @RequestBody AdminSmMessage t)
            throws URISyntaxException {
        log.debug("REST request to update Object : {}", t);

        ResultDto<AdminSmMessage> resultDto = new ResultDto<>();
        try {
            AdminSmMessage result = this.adminSmMessageService.updateWithCache(t);
            resultDto.setData(result);
            resultDto.setMessage("更新系统提示信息成功!");
        } catch (Exception e) {
            resultDto.setCode(500);
            resultDto.setMessage(e.getMessage());
        }

        return resultDto;
    }

    /**
     * @函数名称:createValidate
     * @函数描述:实体类创建，加重复性校验
     * @参数与返回说明:
     * @算法描述:
     */
    @PostMapping("/createvalidate")
    protected ResultDto<AdminSmMessage> createValidate(@ApiParam(value = "Object", required = true) @RequestBody AdminSmMessage t)
            throws URISyntaxException {
        log.debug("REST request to save Object : {}", t);

        int result = this.adminSmMessageService.insert(t);
        ResultDto<AdminSmMessage> resultDto = new ResultDto<AdminSmMessage>(t);
        if (result == 0) {
            resultDto.setCode(MessageConstants.RET_CODE_20100004);
            //resultDto.setMessage(MessageProvider.getMessage("2000").getMessage());
            resultDto.setMessage("信息码已存在，请勿重复");
        } else {
            this.adminSmMessageService.createWithCache(t);
            resultDto.setMessage("新增信息码成功!");
        }
        return resultDto;
    }


}
